/**
 *   Copyright 2008 Alexey Vishentsev
 *
 *   Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 * 
 */

package com.gwtspreadsheetinput.gwt.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.RootPanel;

public class GridDataManager {
	private int cols;
	private List<ControlWrapper[]> rows;
	private String gridContainerId;
	private JsArrayString colIds;
	
	public GridDataManager(String gridId,JsArrayString colIds){
		this.gridContainerId = gridId;
		this.colIds = colIds;
		cols = colIds.length();
		rows = new ArrayList<ControlWrapper[]>();
		int row = 0;
	    boolean rowAdded = false;
	    do {
	    	rowAdded = false;
	    	ControlWrapper[] wrappers = new ControlWrapper[cols];
	    	for(int i = 0; i < cols; i++) {
	    		String elemId = getControlId(row, i);
	    		Element elem = DOM.getElementById(elemId);
	    		if (elem != null && "input".equalsIgnoreCase(elem.getTagName())
	    				&& "hidden".equalsIgnoreCase(elem.getAttribute("type"))) {
	    			
	    			wrappers[i] = new ControlWrapper(elem);
	    			if (!rowAdded) {
	    				rowAdded = true;
	    				rows.add(wrappers);
	    			}
	    		}
	    	}
	    	row++;
	    } while(rowAdded);
	}
	
	private String getControlId(int row , int col){
		return gridContainerId + ':' + row +':' + colIds.get(col);
	}
	
	public int getCurrentRowCount() {
		return rows.size();
	}
	
	public String getValue(int row, int col){
		if (row < 0 || row >= rows.size() || col < 0 || col >= cols) {
			return "";
		}
		ControlWrapper[] rowWrappers = rows.get(row);
		ControlWrapper cellControl = rowWrappers[col];
	    if (cellControl == null) {
	    	return "";
	    }
	    return cellControl.getValue();
	}
	
	public void setValue(int row, int col, String value) {
		if (col < 0 || col > cols || row < 0) {
			return;
		}
		if (row >= rows.size()) {
			RootPanel panel = RootPanel.get(gridContainerId);
			for(int r = rows.size(); r <= row; r++) {
				ControlWrapper[] controls = new ControlWrapper[cols];
				rows.add(controls);
				for (int i = 0; i < cols; i++) {
					String controlId = getControlId(r, i);
					Hidden hidden = new Hidden(controlId,"");
					hidden.setID(controlId);
					panel.add(hidden);
					controls[i] = new ControlWrapper(hidden.getElement());
				}
			}
		}
		ControlWrapper[] controls = rows.get(row);
		ControlWrapper control = controls[col];
		if (control == null) {
			RootPanel panel = RootPanel.get(gridContainerId);
			String controlId = getControlId(row, col);
			Hidden hidden = new Hidden(controlId,"");
			hidden.setID(controlId);
			panel.add(hidden);
			control = new ControlWrapper(hidden.getElement());
			controls[col] = control;
		}
		control.setValue(value);
	}
}
